/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hcmus.fit.master.sevensea.sprite.ship;

import hcmus.fit.master.sevensea.background.Sea;
import hcmus.fit.master.sevensea.core.AbstractSprite;
import hcmus.fit.master.sevensea.core.Constant;

import java.util.List;
import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Point;

/**
 * 
 * @author hvp
 */
public class GoodShip extends WarShip {
    public static List<List<Bitmap>> Resource;
    public static int NumOfStatus;

    public GoodShip(Sea seamap, int x, int y) {
        super(seamap, x, y);
        setFrames(Resource.get(Status));
        setNbOfFrame(getFrames().size());
        FrameLibrary = Resource;
    }

    public GoodShip(GoodShip g) {
        super(g);
    }

    public void Shoot(AbstractSprite[][] items) {
        // Cannons = new List<Cannon>();
        List<Point> targets = GetDestination(items); // cần xem lại
        int i = 0;
        for (int j = 0; j < targets.size(); j++) {
            Point target = (Point) targets.get(j);
            if (target.x != X || target.y != Y) {
                Cannon cannon = Cannons[i];
                cannon.State = CannonState.KillEnemyFlying;
                cannon.InitPosition(X, Y);
                cannon.DestX = target.x;
                cannon.DestY = target.y;

                getSeaMap().items.add(cannon);
                getSeaMap().busy++;
                i++;
            }
        }
        if (i <= 0)
            getSeaMap().busy--;
        else {
            // Cannon.Sound.Play();
            // try{
            // Cannon.Sound.start();
            // }
            // catch(Exception ex){}
        }
    }

    public boolean update() {
        boolean whirl = false;
        switch (State) {
            case ShipState.Idle:
                break;
            case ShipState.Moving:
                if (FinishMove()) {
                    boolean ok = true;
                    State = ShipState.Idle;
                    X = (getRealPos().x) / Constant.CELL_WIDTH;
                    Y = (getRealPos().y) / Constant.CELL_HEIGHT;
                    if (getSeaMap().map[Y][X] != null) {
                        if (getSeaMap().map[Y][X].isIsland() || getSeaMap().map[Y][X].isRubble()) {
                            getSeaMap().items.remove(this);
                            getSeaMap().createExplosion(X, Y);
                        } else if (getSeaMap().map[Y][X].isShip()) {
                            getSeaMap().items.remove(this);
                            getSeaMap().items.remove(getSeaMap().map[Y][X]);
                            getSeaMap().createRubble(X, Y);
                            getSeaMap().createExplosion(X, Y);
                        } else if (getSeaMap().map[Y][X].isWhirl()) {
                            // move to another place
                            ok = false;
                            whirl = true;
                            DoWhirl();
                        }
                    } else {
                        getSeaMap().map[Y][X] = this;
                    }
                    if (ok) {
                        getSeaMap().enemyMove();
                    }
                } else {
                    Move();
                }
                break;
            case ShipState.Rotating:
                if (FinishRotate()) {
                    State = ShipState.Moving;
                    getSeaMap().map[Y][X] = null;
                } else {
                    Rotate();
                }
                break;
            case ShipState.Whirling:
                whirl = true;
                setIndex(getIndex() + Delta);
                if (getIndex() >= ((List<Bitmap>) ShipWhirl.get(0)).size()) {
                    setIndex(((List<Bitmap>) ShipWhirl.get(0)).size() - 1);
                    Delta = -Delta;
                    Random r = new Random();
                    boolean run;
                    int x, y;
                    do {
                        run = false;
                        x = r.nextInt(Constant.Width);
                        y = r.nextInt(Constant.Height);
                        if (getSeaMap().map[y][x] != null) {
                            run = true;
                        }
                    } while (run);
                    moveImmediate(x, y);
                }
                if (getIndex() <= 0) {
                    setIndex(0);
                    StopWhirl();
                }
                break;
        }
        if (!whirl)
            setIndex((getIndex() + 1) % getNbOfFrame());
        return true;
    }

    public boolean isEnemy() {
        return false;
    }
}
